Application = (function() {

    function Application() {
        var _this = this;
        this.pages = {};
        this.mobile = new Mobile();
        this.bootstrap = new Bootstrap();
        this.config = new Config();
        this.mobile.setOnLoadListener(function(){
            _this.mobile.cacheupdated = true;
            _this.load();
        });

        this.mobile.setOnCachedListener(function(e) {
            _this.load();
        });
        this.mobile.setOnCacheUpdatedListener(function(e) {
            _this.load();
        });
        this.mobile.setOnCacheLoadedListener(function(e) {
            _this.load();
        });
        try{
            Touch.android = this.mobile.checker.get().android || true;
        }catch (e){
            throw e;
        }
    }

    Application.prototype.setOnLoadListener = function(listener) {
        this.loadListener = listener;
    };

    Application.prototype.load = function() {
        var _this = this;
        this.config.setOnLoadListener(function() {
            if(_this.loadListener)
                _this.loadListener.call(this);
			_this.setTitle(_this.config.data.title);
			_this.setVersion(_this.config.data.version);
			_this.createPages(_this.config.data.pages);
        });
        this.bootstrap.setOnReadyListener(function() {
            _this.config.load(_this.mobile.cacheupdated);
        });
        this.bootstrap.load(this.mobile.cacheupdated);
    };

	Application.prototype.setTitle = function(text){
		this._title = document.title = text;
	};

	Application.prototype.getTitle = function(){
		return this._title;
	};

	Application.prototype.setVersion = function(version){
		this.mobile.wrapper.attr({"data-version": this._version = version});
	};

	Application.prototype.getVersion = function(){
		return this._version;
	};

	Application.prototype.createPages = function(pages){
        var _i, _page, page;
        for(_i = 0; _i < pages.length; _i++){
            _page = pages[_i];
            page = this.pages[_page.name] = new window[_page["class"]](this);
            if(_page.visible && _page.visible === true){
                if(_page.current && _page.visible === true){
                    page.show();
                }else{
                    (this._currentPage = page).show();
                }
            }
        }
    };

    /**
     * Show page
     * @param intent {Intent}
     */
    Application.prototype.showPage = function(intent){
        var _name = intent.getName(),
            _translation = intent.getAnimation();
        if(this._currentPage = this.pages[_name]) {
            this._currentPage.setAnimation(_translation);
            this._currentPage.show(intent);
        }
    };

    /**
     * Hide page
     * @param intent {Intent}
     */
    Application.prototype.hidePage = function(intent){
        var _name = intent.getName(),
            _translation = intent.getAnimation(),
            page = this.pages[_name];

        if(page && page === this._currentPage)  this._currentPage = null;
        page.setAnimation(_translation);
        page.hide(intent);
    };

    Application.prototype.getCurrentPage = function(){
        return this._currentPage;
    };

    return Application;

})();
